package RefactoringA1.src;

/**
 * #Refactored Completed
 * 
 * @author christopher.witcher
 * @author Thomas Nunn
 */
public class CheckingAccount 
{
	private CustomerData fData;
	private boolean fDebitAllowed;
	private double fDebitAmount;
	private double fBalance;
	private static double costs = 1; // per period there is a cost associated with this account
									// that equals 1 pound
	private static double debitRate = 4.7;
	private CreditCard fCreditCard = null;
	private static int fNextAccountNumber = 0;
	
	/**
	 * #Refactored
	 * 
	 * There were two Constructors which were very redundant
	 * added a statement to set if the fDebitAllowed is true or false;
	 * 
	 * @param name
	 * @param street
	 * @param zipCode
	 * @param town
	 * @param country
	 * @param debit
	 */
	public CheckingAccount(CustomerData the_customer, float debit)
	{
		fData = new CustomerData(the_customer.getfName(), the_customer.getfStreet(), the_customer.getfZipCode(), the_customer.getfTown(), the_customer.getfCountry(), fNextAccountNumber);
		fNextAccountNumber++;
		fDebitAllowed = debit > 0 ? true: false;
		fDebitAmount = debit;
	}
	
	public CheckingAccount(int accountnumber)
	{
		fData = new CustomerData(null, null, null, null, null, accountnumber);
	}
	
	public void determineCosts()
	{
		fBalance = fBalance - costs;
		
		if(fBalance < 0)
		{
			fBalance = fBalance - (fBalance * debitRate / 100);
		}
	}
	
	/**
	 * ------Refactored------
	 * 
	 * Eliminated the System.out statements and added their Strings
	 * to the toString.
	 */
	public void print()
	{
		System.out.println(this.toString());
	}
	
	/**
	 * This method needs to be refactored because it contains multiple return statements.
	 * Also it can be changed to one if statment and eliminate too much code.
	 * 
	 * @param amount
	 * @return
	 */
	public boolean withdrawal(double amount)
	{
		boolean output = false;
		
		if((fBalance - amount) < -fDebitAmount)
		{
			fBalance = fBalance - amount;
			output = true;
		}
		
		return output;
	}
	
	public void verseMoney(double amount)
	{
		fBalance += amount;
	}
	
	/**
	 * The Bad smell is too many parameters.
	 * 
	 * This method should be restructured to one if statements and 
	 * let the CreditCard class handle what limit to set on creation.
	 * This should be using the current customers information and we can use our
	 * CustomerData class to access this users information when creating the credit card
	 * and eliminate all the parameters.
	 * 
	 * Also it should have only one return statement.
	 * @return
	 */
	public boolean applyForCreditCard()
	{
		boolean output = false;
		
		if(fBalance > 0)
		{
			fCreditCard = new CreditCard(fData.getfName(), fData.getfStreet(), fData.getfTown(),
					fData.getfZipCode(), fData.getfCountry(), fBalance);
			output = true;
		}
		
		return output;
	}
	
	/**
	 * #Refactored
	 * 
	 * Method added because fDebitAllowed was never used or able 
	 * to access this information.
	 */
	public boolean isDebitAllowed(){
		return this.fDebitAllowed;
	}
	
	public double getBalance()
	{
		return fBalance;
	}
	
	public void increaseBalance(double amount)
	{
		fBalance += amount;
	}
	
	public void decreaseBalance(double amount)
	{
		fBalance -= amount;
	}

	public double getDebitAmount()
	{
		return fDebitAmount;
	}

	public void setBalance(double amount)
	{
		fBalance = amount;	
	}

	/**
	 * ------Refactored------
	 * 
	 * Returns fData.getfAccountNumber() instead of fAccountNumber.
	 */
	public int getAccountNumber() 
	{
		return fData.getfAccountNumber();	
	}
	
	/**
	 * ------Refactored------
	 * 
	 * Added the @Override and calls to fData.getfAccountNumber().
	 */
	@Override
	public boolean equals(Object o)
	{
		if(fData.getfAccountNumber() == ((CheckingAccount)o).fData.getfAccountNumber())
			return true;
		else
			return false;
				
	}
	
	public CreditCard getCreditCard(){
		return this.fCreditCard;
	}
	
	/**
	 * ------Refactored------
	 * 
	 * This method added to eliminate the System.out statements that were
	 * in the print() method.
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(fData.toString());
		sb.append("\nBalance:  " + fBalance);
		sb.append("\n**************************************");
		return sb.toString();
	}
}
